#include "SoundManager.h"


SoundManager::SoundManager(void)
{
	mMusicVolume = mSoundVolume = INIT_VOLUME ;
}

SoundManager::~SoundManager(void)
{
}

SoundManager::SoundManager(const SoundManager &mm)
{
}

SoundManager* SoundManager::operator=(const SoundManager &mm)
{
	return this;
}

SoundManager* SoundManager::Instance()
{
	static SoundManager instance;

	return &instance;
}

void SoundManager::Update(void)
{
	mSoundSystem->update();
}

void SoundManager::LoadSound(char *filename, unsigned int soundID, float relativeVolume)
{
	SoundData *sd = new SoundData;
	mSoundSystem->createSound(filename, FMOD_DEFAULT, 0, &sd->sound);
	sd->id = soundID;
	sd->vol = relativeVolume;
	mSoundList.push_back(sd);
}

void SoundManager::LoadMusic(char *filename, unsigned int musicID, float relativeVolume)
{
	MusicData *md = new MusicData;
	mSoundSystem->createStream(filename, FMOD_LOOP_NORMAL | FMOD_2D | FMOD_HARDWARE, 0, &md->stream);
	md->id = musicID;
	md->vol = relativeVolume;
	mMusicList.push_back(md);

	md->m_Playing = false ;
}


void SoundManager::SoundPlay(unsigned int soundID)
{
	Sounds::iterator s = mSoundList.begin();
	while (s != mSoundList.end())
	{
		if ((*s)->id == soundID)
		{
			mSoundSystem->playSound(FMOD_CHANNEL_FREE, (*s)->sound, false, &(*s)->channel);
			(*s)->channel->setVolume(mSoundVolume*(*s)->vol);
			return;
		}
		else
			s++;
	}
}

void SoundManager::SoundStop(unsigned int soundID)
{
	Sounds::iterator s = mSoundList.begin();
	while (s != mSoundList.end())
	{
		if ((*s)->id == soundID)
		{
			(*s)->channel->stop();
			return;
		}
		else
			s++;
	}
}

void SoundManager::SoundStop(void)
{
	Sounds::iterator s = mSoundList.begin();
	while (s != mSoundList.end())
	{
		(*s)->channel->stop();
		s++;
	}
}

void SoundManager::MusicPlay(unsigned int musicID)
{
	Musics::iterator m = mMusicList.begin();
	while (m != mMusicList.end())
	{
		if ((*m)->id == musicID && !(*m)->m_Playing)
		{
			mSoundSystem->playSound(FMOD_CHANNEL_FREE, (*m)->stream, false, &(*m)->channel);
			(*m)->channel->setVolume(mMusicVolume*(*m)->vol);
			(*m)->m_Playing = true ;
			return;
		}
		else
			m++;
	}
}

void SoundManager::MusicStop(unsigned int musicID)
{
	Musics::iterator m = mMusicList.begin();
	while (m != mMusicList.end())
	{
		if ((*m)->id == musicID)
		{
			(*m)->channel->stop();
			(*m)->m_Playing = false ;
			return;
		}
		else
			m++;
	}
} 

void SoundManager::MusicStop(void)
{
	Musics::iterator m = mMusicList.begin();
	while (m != mMusicList.end())
	{
		(*m)->channel->stop();
		(*m)->m_Playing = false ;
		m++;
	}
} 

void SoundManager::Initialize(void)
{
	FMOD::System_Create(&mSoundSystem);
	mSoundSystem->setOutput(FMOD_OUTPUTTYPE_DSOUND);
	mSoundSystem->init(32, FMOD_INIT_NORMAL, 0);
}

void SoundManager::Release(void)
{
	
	while (mSoundList.size() > 0)
	{
		Sounds::iterator s = mSoundList.begin();
		(*s)->sound->release();
		delete *s;
		mSoundList.erase(s);
	}

	while (mMusicList.size() > 0)
	{
		Musics::iterator m = mMusicList.begin();
		(*m)->stream->release();
		delete *m;
		mMusicList.erase(m);
	}

}

void SoundManager::SetVolume(float soundVol, float musicVol) 
{
	mSoundVolume = soundVol; 
	mMusicVolume = musicVol;

	Sounds::iterator s = mSoundList.begin();
	while (s != mSoundList.end())
	{
		(*s)->channel->setVolume(mSoundVolume*(*s)->vol);
		s++;
	}

	Musics::iterator m = mMusicList.begin();
	while (m != mMusicList.end())
	{
		(*m)->channel->setVolume(mMusicVolume*(*m)->vol);
		m++;
	}
}